<?php

/**
 * Interface DutyChain
 */
interface DutyChain
{
    /**
     * handle
     * @return bool
     */
    public function handle() : bool;
}

class Request
{
    public static function init()
    {
        return new static();
    }
    public $day = 5;
}

/**
 * Class Handler
 */
abstract class Handler implements DutyChain
{
    /**
     * 请求对象
     * @var \Request $request
     */
    protected $request;
    
    /**
     * 更高级别的处理者
     * @var Handler
     */
    protected $successor;
    
    /**
     * Handler constructor.
     * @param \Request $request
     */
    public function __construct(\Request $request)
    {
        $this->request = $request;
    }
    
    /**
     * init 静态初始化对象
     * @param \Request $request
     * @static
     * @return static
     */
    public static function init(\Request $request)
    {
        return new static($request);
    }
    
    /**
     * handle 处理业务逻辑
     * @return bool
     * @throws \ErrorException
     */
    public function handle() : bool
    {
        if($this->canHandle()) {
            return $this->processing();
        }
        if(null !== $this->successor){
            return $this->successor->handle();
        }
        throw new ErrorException('没有更多人可以处理你的请求');
    }
    
    /**
     * canHandle 当前处理者是否可以处理逻辑
     * @return bool
     */
    abstract protected function canHandle() : bool;
    
    /**
     * processing 处理逻辑
     * @return bool
     */
    abstract protected function processing() : bool;
    
    /**
     * 设置更高级别的处理者
     * @param \Handler $successor
     */
    public function setSuccessor(\Handler $successor) : void
    {
        $this->successor = $successor;
    }
}

class Headman extends Handler
{
    /**
     * canHandle 当前处理者是否可以处理逻辑
     * @return bool
     */
    protected function canHandle() : bool
    {
        return $this->request->day < 2;
    }
    
    /**
     * processing 处理逻辑
     * @return bool
     */
    protected function processing() : bool
    {
        echo __CLASS__.'审批了你的请求' . PHP_EOL;
        return true;
    }
}

class DepartmentSupervisor extends Handler
{
    /**
     * canHandle 当前处理者是否可以处理逻辑
     * @return bool
     */
    protected function canHandle() : bool
    {
        return $this->request->day < 3;
    }
    
    /**
     * processing 处理逻辑
     * @return bool
     */
    protected function processing() : bool
    {
        echo __CLASS__.'审批了你的请求' . PHP_EOL;
        return true;
    }
}

class Boss extends Handler
{
    /**
     * canHandle 当前处理者是否可以处理逻辑
     * @return bool
     */
    protected function canHandle() : bool
    {
        return $this->request->day < 7;
    }
    
    /**
     * processing 处理逻辑
     * @return bool
     */
    protected function processing() : bool
    {
        echo __CLASS__.'审批了你的请求' . PHP_EOL;
        return true;
    }
}

$dutyChainList = [
    new Headman(Request::init()),
    new DepartmentSupervisor(Request::init()),
    new Boss(Request::init())
];
foreach ($dutyChainList as $key => $item)
{
    if(isset($dutyChainList[$key + 1])) {
        $item->setSuccessor($dutyChainList[$key + 1]);
    }
}
/** @var \Handler $firstHandler */
$firstHandler = reset($dutyChainList);
try {
    $firstHandler->handle();
} catch (Exception $e) {
    echo $e->getMessage();
}